# Summary: Creates Appendix E

######################################################
#-------------------Appendix E ----------------------#
######################################################

options(scipen=999)
rm(list = ls())

# Load libraries
library(rddensity)
library(rdrobust)
library(lmtest)
library(sandwich)

# Set WD
# setwd('~/Dataverse/')

# Source functions
source('./Code/fun_analysis.R')

# Load Data
load(file = "./Data/d.RData")

# Period
d$period <- 'weak'
d$period[d$ANO_ELEICAO > 2007] <- 'strong'
d$period[d$ANO_ELEICAO > 2015] <- 'moderate'

# Centralized and Decentralized parties
d$centralized <- as.numeric(d$SIGLA_PARTIDO %in% c("PC do B", "PT", "PSDB", "PPS", "PFL", "DEM", "PL", "PR", "PDT"))

# Get codeyear for where a centralized and decentralized party won
centParties <- d$codeyear[(d$SIGLA_PARTIDO %in% c("PC do B", "PT", "PSDB", "PPS", "PFL", "DEM", "PL", "PR", "PDT")) & d$eleito == 1]

# Keep where centralized parties won
d <- subset(d, codeyear %in% centParties)

# Select mixed-gender races
d$female <- as.numeric(d$CODIGO_SEXO == 4)
mixed <- aggregate(d$female, list(d$codeyear), sum)
names(mixed) <- c('codeyear', 'mixed')
mixed$mixed <- ifelse(mixed$mixed == 1, 1, 0)
d <- merge(d, mixed, by = 'codeyear')
# Subset to Mixed Races and parties ran both male and female candidates
d2 <- subset(d, mixed == 1 & nonZeroDF == 2)
# Create running variable
d2$run_var_fem <- ifelse(d2$position == 1 & d2$CODIGO_SEXO == 4
                         , d2$diff_cand * 1, d2$diff_cand * -1)
d2$run_var_fem[d2$mayorFem == 0] <- NA
d2$run_var_male <- ifelse(d2$position == 1 & d2$CODIGO_SEXO == 2
                          , d2$diff_cand * 1, d2$diff_cand * -1)
d2$run_var_male[d2$mayorFem == 1] <- NA
table(is.na(d2$run_var_male))
table(is.na(d2$run_var_fem))

###########################################################################
#-------------------Appendix E.1: Descriptive Stats ----------------------#
###########################################################################

# TABLE E.16

sink("./AppendixEResults/tableE16.txt")
print("TABLE E.16: Descriptive Statistics - Only Centralized Parties")
summary(d2$avg_male_DF[d2$period == 'weak' & is.na(d2$run_var_fem)]); length(d2$avg_male_DF[d2$period == 'weak' & is.na(d2$run_var_fem)]); sd(d2$avg_male_DF[d2$period == 'weak' & is.na(d2$run_var_fem)])
summary(d2$avg_fem_DF[d2$period == 'weak' & is.na(d2$run_var_fem)]); length(d2$avg_fem_DF[d2$period == 'weak' & is.na(d2$run_var_fem)]); sd(d2$avg_fem_DF[d2$period == 'weak' & is.na(d2$run_var_fem)])
summary(d2$avg_male_DF[d2$period == 'strong' & is.na(d2$run_var_fem)]); length(d2$avg_male_DF[d2$period == 'strong' & is.na(d2$run_var_fem)]); sd(d2$avg_male_DF[d2$period == 'strong' & is.na(d2$run_var_fem)])
summary(d2$avg_fem_DF[d2$period == 'strong' & is.na(d2$run_var_fem)]); length(d2$avg_fem_DF[d2$period == 'strong' & is.na(d2$run_var_fem)]); sd(d2$avg_fem_DF[d2$period == 'strong' & is.na(d2$run_var_fem)])
summary(d2$avg_male_DF[d2$period == 'moderate' & is.na(d2$run_var_fem)]); length(d2$avg_male_DF[d2$period == 'moderate' & is.na(d2$run_var_fem)]); sd(d2$avg_male_DF[d2$period == 'moderate' & is.na(d2$run_var_fem)])
summary(d2$avg_fem_DF[d2$period == 'moderate' & is.na(d2$run_var_fem)]); length(d2$avg_fem_DF[d2$period == 'moderate' & is.na(d2$run_var_fem)]); sd(d2$avg_fem_DF[d2$period == 'moderate' & is.na(d2$run_var_fem)])

summary(d2$avg_male_DF[d2$period == 'weak' & is.na(d2$run_var_male)]); length(d2$avg_male_DF[d2$period == 'weak' & is.na(d2$run_var_male)]); sd(d2$avg_male_DF[d2$period == 'weak' & is.na(d2$run_var_male)])
summary(d2$avg_fem_DF[d2$period == 'weak' & is.na(d2$run_var_male)]); length(d2$avg_fem_DF[d2$period == 'weak' & is.na(d2$run_var_male)]); sd(d2$avg_fem_DF[d2$period == 'weak' & is.na(d2$run_var_male)])
summary(d2$avg_male_DF[d2$period == 'strong' & is.na(d2$run_var_male)]); length(d2$avg_male_DF[d2$period == 'strong' & is.na(d2$run_var_male)]); sd(d2$avg_male_DF[d2$period == 'strong' & is.na(d2$run_var_male)])
summary(d2$avg_fem_DF[d2$period == 'strong' & is.na(d2$run_var_male)]); length(d2$avg_fem_DF[d2$period == 'strong' & is.na(d2$run_var_male)]); sd(d2$avg_fem_DF[d2$period == 'strong' & is.na(d2$run_var_male)])
summary(d2$avg_male_DF[d2$period == 'moderate' & is.na(d2$run_var_male)]); length(d2$avg_male_DF[d2$period == 'moderate' & is.na(d2$run_var_male)]); sd(d2$avg_male_DF[d2$period == 'moderate' & is.na(d2$run_var_male)])
summary(d2$avg_fem_DF[d2$period == 'moderate' & is.na(d2$run_var_male)]); length(d2$avg_fem_DF[d2$period == 'moderate' & is.na(d2$run_var_male)]); sd(d2$avg_fem_DF[d2$period == 'moderate' & is.na(d2$run_var_male)])
sink()


######################################################################
#-------------------Appendix E.2: Manipulation ----------------------#
######################################################################

summary(beforeMen <- rddensity(X = subset(d2, !(ANO_ELEICAO %in% c(2008, 2012)))$run_var_male, c = 0)) # With massPoints
summary(afterMen <- rddensity(X = subset(d2, ANO_ELEICAO %in% c(2008, 2012))$run_var_male, c = 0)) # With massPoints
summary(after2018Men <- rddensity(X = subset(d2, ANO_ELEICAO %in% c(2016))$run_var_male, c = 0)) # With massPoints
# Result: No sorting

# Density Plot 
# FIGURE E.38 (a and c) and FIGURE E.39 (a)
pdf('./AppendixEResults/figE38_a_c_fig39_a.pdf')
hist(subset(d2, !(ANO_ELEICAO %in% c(2008, 2012)))$run_var_male[abs(subset(d2, !(ANO_ELEICAO %in% c(2008, 2012)))$run_var_male)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
	main = "", xlab = "Difference in Vote Share", ylim = c(0, 20))
abline(v = 0, col = "red", lwd = 1.5)
text(x = 6, y = 17, 
	labels = paste("p-value density test = ", 
		round(beforeMen$test$p_jk, 4)), cex = 1)
hist(subset(d2, (ANO_ELEICAO %in% c(2008, 2012)))$run_var_male[abs(subset(d2, (ANO_ELEICAO %in% c(2008, 2012)))$run_var_male)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
	main = "", xlab = "Difference in Vote Share",  ylim = c(0, 20))
abline(v = 0, col = "red", lwd = 1.5)
text(x = 6, y = 17, 
	labels = paste("p-value density test = ", 
		round(afterMen$test$p_jk, 4)), cex = 1)
hist(subset(d2, (ANO_ELEICAO %in% c(2016)))$run_var_male[abs(subset(d2, (ANO_ELEICAO %in% c(2016)))$run_var_male)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
	main = "", xlab = "Difference in Vote Share",  ylim = c(0, 10))
abline(v = 0, col = "red", lwd = 1.5)
text(x = 6, y = 9, 
	labels = paste("p-value density test = ", 
		round(afterMen$test$p_jk, 4)), cex = 1)
dev.off()

summary(beforeWomen <- rddensity(X = subset(d2, !(ANO_ELEICAO %in% c(2008, 2012)))$run_var_fem, c = 0)) # With massPoints
summary(afterWomen <- rddensity(X = subset(d2, (ANO_ELEICAO %in% c(2008, 2012)))$run_var_fem, c = 0)) # With massPoints
summary(after18Women <- rddensity(X = subset(d2, (ANO_ELEICAO %in% c(2016)))$run_var_fem, c = 0)) # With massPoints

# Result: No sorting
# Density Plot 
# FIGURE E.38 (b and d) and FIGURE E.39 (b)
pdf('./AppendixEResults/figE38_b_d_fig39_b.pdf')
hist(subset(d2, !(ANO_ELEICAO %in% c(2008, 2012)))$run_var_fem[abs(subset(d2, !(ANO_ELEICAO %in% c(2008, 2012)))$run_var_fem)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
	main = "", xlab = "Difference in Vote Share", ylim = c(0, 15))
abline(v = 0, col = "red", lwd = 1.5)
text(x = 6, y = 15, 
	labels = paste("p-value density test = ", 
		round(beforeWomen$test$p_jk, 4)), cex = 1)
hist(subset(d2, (ANO_ELEICAO %in% c(2008, 2012)))$run_var_fem[abs(subset(d2, (ANO_ELEICAO %in% c(2008, 2012)))$run_var_fem)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
	main = "", xlab = "Difference in Vote Share",  ylim = c(0, 15))
abline(v = 0, col = "red", lwd = 1.5)
text(x = 6, y = 12.5, 
	labels = paste("p-value density test = ", 
		round(afterWomen$test$p_jk, 4)), cex = 1)
hist(subset(d2, (ANO_ELEICAO %in% c(2016)))$run_var_fem[abs(subset(d2, (ANO_ELEICAO %in% c(2016)))$run_var_fem)<=(13.039 - 10.873)/2 + 10.873], breaks = 50, # at the bw
	main = "", xlab = "Difference in Vote Share",  ylim = c(0, 15))
abline(v = 0, col = "red", lwd = 1.5)
text(x = 6, y = 6.5, 
	labels = paste("p-value density test = ", 
		round(afterWomen$test$p_jk, 4)), cex = 1)
dev.off()

######################################################################
#-------------------Appendix E.3: Balance Test ----------------------#
######################################################################

################################
########## Municipal-Party Level
################################

##### Lag DV
# % of vote share male
d2$perc_party_mun_male_placebo1DF <- d2$perc_party_mun_placebo1DF - d2$perc_party_mun_fem_placebo1DF
# N of male candidates
d2$n_party_Malecandidates_placebo1DF <- d2$n_party_candidates_placebo1DF - d2$n_party_Femcandidates_placebo1DF
# Avg vote share male
d2$avg_male_placebo1DF <- d2$perc_party_mun_male_placebo1DF/(d2$n_party_Malecandidates_placebo1DF)
# Avg vote share female
d2$avg_fem_placebo1DF <- d2$perc_party_mun_fem_placebo1DF/(d2$n_party_Femcandidates_placebo1DF)

# Find municipalities that should be included (both parties had male and female candidates)
valid <- aggregate(d2$n_party_Malecandidates_placebo1DF > 0 & d2$n_party_Femcandidates_placebo1DF>0 & (!is.na(d2$avg_male_placebo1DF) & !is.na(d2$avg_fem_placebo1DF)), list(d2$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dPlacebo <- subset(d2, codeyear %in% valid_place)

######### mayor = men
# Weak
b1 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_male_placebo1DF
		, x = subset(dPlacebo, period == 'weak')$run_var_male
		, p = 1
		, c = 0	
		, cluster = subset(dPlacebo, period == 'weak')$codeyear
		, all = TRUE)
summary(b1)
b2 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_fem_placebo1DF
		, x = subset(dPlacebo,  period == 'weak')$run_var_male
		, p = 1
		, c = 0	
		, cluster = subset(dPlacebo, period == 'weak')$codeyear
		, all = TRUE)
summary(b2)
# Strong
b3 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_male_placebo1DF
      , x = subset(dPlacebo, period == 'strong')$run_var_male
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'strong')$codeyear
      , all = TRUE)
summary(b3)
b4 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_fem_placebo1DF
      , x = subset(dPlacebo,  period == 'strong')$run_var_male
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'strong')$codeyear
      , all = TRUE)
summary(b4)
# Moderate
b5 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_male_placebo1DF
      , x = subset(dPlacebo, period == 'moderate')$run_var_male
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'moderate')$codeyear
      , all = TRUE)
summary(b5)
b6 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_fem_placebo1DF
      , x = subset(dPlacebo,  period == 'moderate')$run_var_male
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'moderate')$codeyear
      , all = TRUE)
summary(b6)

######### mayor = women 
# Weak
b7 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_male_placebo1DF
      , x = subset(dPlacebo, period == 'weak')$run_var_fem
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'weak')$codeyear
      , all = TRUE)
summary(b7)
b8 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_fem_placebo1DF
      , x = subset(dPlacebo,  period == 'weak')$run_var_fem
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'weak')$codeyear
      , all = TRUE)
summary(b8)
# Strong
b9 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_male_placebo1DF
      , x = subset(dPlacebo, period == 'strong')$run_var_fem
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'strong')$codeyear
      , all = TRUE)
summary(b9)
b10 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_fem_placebo1DF
      , x = subset(dPlacebo,  period == 'strong')$run_var_fem
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'strong')$codeyear
      , all = TRUE)
summary(b10)
# Moderate
b11 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_male_placebo1DF
      , x = subset(dPlacebo, period == 'moderate')$run_var_fem
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'moderate')$codeyear
      , all = TRUE)
summary(b11)
b12 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_fem_placebo1DF
      , x = subset(dPlacebo,  period == 'moderate')$run_var_fem
      , p = 1
      , c = 0	
      , cluster = subset(dPlacebo, period == 'moderate')$codeyear
      , all = TRUE)
summary(b12)


#####--------------- Lag DV State Deputy---------------##### 
# % of vote share male
d2$temp <- ifelse(is.na(d2$perc_party_mun_fem_placebo1DE), 0, d2$perc_party_mun_fem_placebo1DE)
d2$perc_party_mun_male_placebo1DE <- d2$perc_party_mun_placebo1DE - d2$temp
# N of male candidates
d2$temp <- ifelse(is.na(d2$n_party_Femcandidates_placebo1DE), 0, d2$n_party_Femcandidates_placebo1DE)
d2$n_party_Malecandidates_placebo1DE <- d2$n_party_candidates_placebo1DE - d2$n_party_Femcandidates_placebo1DE
# Avg vote share male
d2$avg_male_placebo1DE <- d2$perc_party_mun_male_placebo1DE/(d2$n_party_Malecandidates_placebo1DE)
# Avg vote share female
d2$avg_fem_placebo1DE <- d2$perc_party_mun_fem_placebo1DE/(d2$n_party_Femcandidates_placebo1DE)

# Find municipalities that should be included (both parties had male and female candidates)
valid <- aggregate(d2$n_party_Malecandidates_placebo1DE > 0 & d2$n_party_Femcandidates_placebo1DE>0 & (!is.na(d2$avg_male_placebo1DE) & !is.na(d2$avg_fem_placebo1DE)), list(d2$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dPlacebo <- subset(d2, codeyear %in% valid_place)

######### mayor = men
# Weak
b13 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_male_placebo1DE
                 , x = subset(dPlacebo, period == 'weak')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b13)
b14 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_fem_placebo1DE
                 , x = subset(dPlacebo,  period == 'weak')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b14)
# Strong
b15 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_male_placebo1DE
                 , x = subset(dPlacebo, period == 'strong')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b15)
b16 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_fem_placebo1DE
                 , x = subset(dPlacebo,  period == 'strong')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b16)
# Moderate
b17 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_male_placebo1DE
                 , x = subset(dPlacebo, period == 'moderate')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b17)
b18 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_fem_placebo1DE
                 , x = subset(dPlacebo,  period == 'moderate')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b18)

######### mayor = women 
# Weak
b19 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_male_placebo1DE
                 , x = subset(dPlacebo, period == 'weak')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b19)
b20 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_fem_placebo1DE
                 , x = subset(dPlacebo,  period == 'weak')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b20)
# Strong
b21 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_male_placebo1DE
                 , x = subset(dPlacebo, period == 'strong')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b21)
b22 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_fem_placebo1DE
                 , x = subset(dPlacebo,  period == 'strong')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b22)
# Moderate
b23 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_male_placebo1DE
                 , x = subset(dPlacebo, period == 'moderate')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b23)
b24 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_fem_placebo1DE
                 , x = subset(dPlacebo,  period == 'moderate')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b24)


#####--------------- Local Council---------------##### 

load(file = "./Data/d_balance.RData")

# Combine datasets
d2 <- merge(d2, d_balance, by = c('codeyear', 'NUMERO_PARTIDO'), all.x = TRUE)

#### Variable for balance test
# Perct of vote share
d2$perc_party_mun_placebo_Ver <- (d2$total_votes_party_vereador/d2$total_votes_district_vereador) * 100
d2$perc_party_mun_fem_placebo_Ver <- (d2$total_votes_party_fem_vereador/d2$total_votes_district_vereador) * 100
d2$temp <- ifelse(is.na(d2$perc_party_mun_fem_placebo_Ver), 0, d2$perc_party_mun_fem_placebo_Ver)
d2$perc_party_mun_male_placebo_Ver <- d2$perc_party_mun_placebo_Ver - d2$temp 
# N of Candidates
d2$temp <- ifelse(is.na(d2$n_candidates_fem_vereador), 0, d2$n_candidates_fem_vereador)
d2$n_candidates_male_vereador <- d2$n_candidates_vereador - d2$temp
# Avg Vote Sahre
d2$avg_vs_male_ver <- (d2$perc_party_mun_male_placebo_Ver/d2$n_candidates_male_vereador) 
d2$avg_vs_fem_ver <- (d2$perc_party_mun_fem_placebo_Ver/d2$n_candidates_fem_vereador) 

# Keep municipalities that had men and women running  
valid <- aggregate((d2$n_candidates_vereador - d2$n_candidates_fem_vereador) > 0 & d2$n_candidates_fem_vereador>0 & (!is.na(d2$avg_vs_male_ver) & !is.na(d2$avg_vs_fem_ver)), list(d2$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dPlacebo <- subset(d2, codeyear %in% valid_place & nonZeroDF == 2)

######### mayor = men
# Weak
b25 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_vs_male_ver
                 , x = subset(dPlacebo, period == 'weak')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b25)
b26 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_vs_fem_ver
                 , x = subset(dPlacebo,  period == 'weak')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b26)
# Strong
b27 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_vs_male_ver
                 , x = subset(dPlacebo, period == 'strong')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b27)
b28 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_vs_fem_ver
                 , x = subset(dPlacebo,  period == 'strong')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b28)
# Moderate
b29 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_vs_male_ver
                 , x = subset(dPlacebo, period == 'moderate')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b29)
b30 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_vs_fem_ver
                 , x = subset(dPlacebo,  period == 'moderate')$run_var_male
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b30)

######### mayor = women 
# Weak
b31 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_vs_male_ver
                 , x = subset(dPlacebo, period == 'weak')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b31)
b32 = rdrobust(y = subset(dPlacebo, period == 'weak')$avg_vs_fem_ver
                 , x = subset(dPlacebo,  period == 'weak')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'weak')$codeyear
                 , all = TRUE)
summary(b32)
# Strong
b33 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_vs_male_ver
                 , x = subset(dPlacebo, period == 'strong')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b33)
b34 = rdrobust(y = subset(dPlacebo, period == 'strong')$avg_vs_fem_ver
                 , x = subset(dPlacebo,  period == 'strong')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'strong')$codeyear
                 , all = TRUE)
summary(b34)
# Moderate
b35 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_vs_male_ver
                 , x = subset(dPlacebo, period == 'moderate')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b35)
b36 = rdrobust(y = subset(dPlacebo, period == 'moderate')$avg_vs_fem_ver
                 , x = subset(dPlacebo,  period == 'moderate')$run_var_fem
                 , p = 1
                 , c = 0	
                 , cluster = subset(dPlacebo, period == 'moderate')$codeyear
                 , all = TRUE)
summary(b36)

# TABLE E.17
ordering = c(1,13,25,2,14,26,
             3,15,27,4,16,28,
             5,17,29,6,18,30,
             7,19,31,8,20,32,
             9,21,33,10,22,34,
             11,23,35,12,24,36)
eval(parse(text = paste0("Estimate = rbind(", paste0(paste0("b", ordering, "[['coef']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("p_value = rbind(", paste0(paste0("b", ordering, "[['pv']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("h = rbind(", paste0(paste0("b", ordering, "[['bws']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_co = rbind(", paste0(paste0("b", ordering, "[['N_h']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_tr = rbind(", paste0(paste0("b", ordering, "[['N_h']][2]"), collapse = ","), ")") 
))
table_e17 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixEResults/tableE17.txt")
print("TABLE E.17: Municipal-Party Level Tests, Only Centralized Party Sample by Party Regimes and
Mayor’s Gender")
table_e17
sink()


################################
########## Individual Level 
################################
load(file = "./Data/balance_ind.RData")


### Add variables
# Note: Unfortunately, we had to use a new version of TSE data in order to create
# the variables for the balance tests. As a result, our sample is slightly different
# Our results are robust to the use of this different sample. Also, these data are
# only available for elections after 1996.
dBalance <- merge(d2, balance_ind, 
                  by.x = c('ANO_ELEICAO.x', 'CODIGO_MUNICIPIO.x', 
                           'NUMERO_PARTIDO'),
                  by.y = c('ANO_ELEICAO', 'SG_UE', 
                           'NR_PARTIDO'), all.x = TRUE)
dBalance$women <- as.numeric(dBalance$CODIGO_SEXO == 4)

# Keep only municipalities that both candidates are available
dBalance$x <- 1
valid <- aggregate(dBalance$x, list(dBalance$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dBalance <- subset(dBalance, codeyear %in% valid_place & nonZeroDF == 2)



############ Mayor is a man
# TABLE E.18
######### Weak Period
# Keep only balanced municipalities  
keep <- names(table(dBalance$codeyear[!is.na(dBalance$business_occ)])[table(dBalance$codeyear[!is.na(dBalance$business_occ)])==2])
b1 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$business_occ,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'& codeyear %in% keep)$codeyear)
summary(b1)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$health_occ)])[table(dBalance$codeyear[!is.na(dBalance$health_occ)])==2])
b2 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$health_occ,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b2)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b3 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$education_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b3)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b4 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$merchant_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b4)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])[table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])==2])
b5 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$farmer_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b5)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])[table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])==2])
b6 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$pubservant_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b6)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])[table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])==2])
b7 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$laywer_pcc,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b7)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$incumbent)])[table(dBalance$codeyear[!is.na(dBalance$incumbent)])==2])
b8 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$incumbent,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b8)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$held_office)])[table(dBalance$codeyear[!is.na(dBalance$held_office)])==2])
b9 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$held_office,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b9)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$ran_office)])[table(dBalance$codeyear[!is.na(dBalance$ran_office)])==2])
b10 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$ran_office,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b10)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b11 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$age,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b11)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$married)])[table(dBalance$codeyear[!is.na(dBalance$married)])==2])
b12 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$married,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b12)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b13 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$highschool,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b13)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b14 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$somecollege,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b14)


######### Strong Period
keep <- names(table(dBalance$codeyear[!is.na(dBalance$business_occ)])[table(dBalance$codeyear[!is.na(dBalance$business_occ)])==2])
b15 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$business_occ,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'& codeyear %in% keep)$codeyear)
summary(b15)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$health_occ)])[table(dBalance$codeyear[!is.na(dBalance$health_occ)])==2])
b16 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$health_occ,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b16)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b17 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$education_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b17)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b18 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$merchant_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b18)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])[table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])==2])
b19 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$farmer_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b19)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])[table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])==2])
b20 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$pubservant_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b20)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])[table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])==2])
b21 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$laywer_pcc,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b21)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$incumbent)])[table(dBalance$codeyear[!is.na(dBalance$incumbent)])==2])
b22 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$incumbent,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b22)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$held_office)])[table(dBalance$codeyear[!is.na(dBalance$held_office)])==2])
b23 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$held_office,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b23)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$ran_office)])[table(dBalance$codeyear[!is.na(dBalance$ran_office)])==2])
b24 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$ran_office,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b24)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b25 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$age,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b25)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$married)])[table(dBalance$codeyear[!is.na(dBalance$married)])==2])
b26 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$married,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b26)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b27 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$highschool,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b27)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b28 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$somecollege,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b28)

######### Moderate Period
keep <- names(table(dBalance$codeyear[!is.na(dBalance$business_occ)])[table(dBalance$codeyear[!is.na(dBalance$business_occ)])==2])
b29 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$business_occ,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'& codeyear %in% keep)$codeyear)
summary(b29)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$health_occ)])[table(dBalance$codeyear[!is.na(dBalance$health_occ)])==2])
b30 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$health_occ,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b30)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b31 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$education_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b31)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b32 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$merchant_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b32)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])[table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])==2])
b33 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$farmer_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b33)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])[table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])==2])
b34 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$pubservant_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b34)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])[table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])==2])
b35 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$laywer_pcc,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b35)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$incumbent)])[table(dBalance$codeyear[!is.na(dBalance$incumbent)])==2])
b36 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$incumbent,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b36)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$held_office)])[table(dBalance$codeyear[!is.na(dBalance$held_office)])==2])
b37 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$held_office,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b37)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$ran_office)])[table(dBalance$codeyear[!is.na(dBalance$ran_office)])==2])
b38 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$ran_office,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b38)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b39 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$age,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b39)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$married)])[table(dBalance$codeyear[!is.na(dBalance$married)])==2])
b40 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$married,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b40)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b41 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$highschool,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b41)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b42 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$somecollege,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b42)


# Campaign Expenditures are only available after 2000 and many candidates do not report
# So, we will do a separate analysis
load(file = './Data/DespesaMunicipal.RData')

# Add Data
dBalance2 <- merge(dBalance, despesa, 
                   by.x = c('CODIGO_MUNICIPIO.x', 'ANO_ELEICAO.x', 'NUMERO_PARTIDO'), 
                   by.y = c('SG_UE', 'ANO_ELEICAO', 'NR_PART'))
# Keep only municipalities that both candidates are available
dBalance2$x <- 1
valid <- aggregate(dBalance2$x, list(dBalance2$codeyear), sum)
valid_place <- na.omit(valid$Group.1[valid$x == 2])
dBalance2 <- subset(dBalance2, codeyear %in% valid_place & nonZeroDF == 2)

# Rescale by 1000
dBalance2$DESPESA <- dBalance2$DESPESA/1000

b43 = rdrobust(y = subset(dBalance2,  period == 'weak')$DESPESA,
                 x = subset(dBalance2,  period == 'weak')$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2,  period == 'weak')$codeyear)
summary(b43)

b44 = rdrobust(y = subset(dBalance2,  period == 'strong')$DESPESA,
                 x = subset(dBalance2,  period == 'strong')$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2,  period == 'strong')$codeyear)
summary(b44)

b45 = rdrobust(y = subset(dBalance2,  period == 'moderate')$DESPESA,
                 x = subset(dBalance2,  period == 'moderate')$run_var_male,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2,  period == 'moderate')$codeyear)
summary(b45)

# TABLE E.18
ordering = c(1:14,43,15:28,44,29:42,45)
eval(parse(text = paste0("Estimate = rbind(", paste0(paste0("b", ordering, "[['coef']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("p_value = rbind(", paste0(paste0("b", ordering, "[['pv']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("h = rbind(", paste0(paste0("b", ordering, "[['bws']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_co = rbind(", paste0(paste0("b", ordering, "[['N_h']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_tr = rbind(", paste0(paste0("b", ordering, "[['N_h']][2]"), collapse = ","), ")") 
))
table_e18 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixEResults/tableE18.txt")
print("TABLE E.18: Individual Level Tests, Only Centralized Party Sample by Party Regimes when the Mayor is a Man")
table_e18
sink()

# TABLE E.19
# Including Covariates
keep <- names(table(dBalance$codeyear[!is.na(dBalance[, c('age', 'farmer_occ', 'laywer_pcc', 'somecollege')])])[table(dBalance$codeyear[!is.na(dBalance[, c('age', 'farmer_occ', 'laywer_pcc', 'somecollege')])])==2])
out.cent.men.opt.weak <- rdrobust(y = subset(dBalance, period == 'weak' & codeyear %in% keep)$avg_male_DF
                                    , x = subset(dBalance, period == 'weak' & codeyear %in% keep)$run_var_male
                                    , covs = cbind(subset(dBalance, period == 'weak' & codeyear %in% keep)$farmer_occ,
                                                   subset(dBalance, period == 'weak' & codeyear %in% keep)$laywer_pcc,
                                                   subset(dBalance, period == 'weak' & codeyear %in% keep)$age, 
                                                   subset(dBalance, period == 'weak' & codeyear %in% keep)$somecollege)                                    
                                    , p = 1
                                    , c = 0	
                                    , cluster = subset(dBalance, period == 'weak' & codeyear %in% keep)$codeyear
                                    , all = TRUE)
out.cent.women.opt.weak <- rdrobust(y = subset(dBalance, period == 'weak' & codeyear %in% keep)$avg_fem_DF
                                      , x = subset(dBalance, period == 'weak' & codeyear %in% keep)$run_var_male
                                      , covs = cbind(subset(dBalance, period == 'weak' & codeyear %in% keep)$farmer_occ,
                                                   subset(dBalance, period == 'weak' & codeyear %in% keep)$laywer_pcc,
                                                   subset(dBalance, period == 'weak' & codeyear %in% keep)$age, 
                                                   subset(dBalance, period == 'weak' & codeyear %in% keep)$somecollege)                                       
                                      , p = 1
                                      , c = 0	
                                      , cluster = subset(dBalance, period == 'weak' & codeyear %in% keep)$codeyear
                                      , all = TRUE)
summary(out.cent.men.opt.weak)
summary(out.cent.women.opt.weak)


# Strong
keep <- names(table(dBalance$codeyear[!is.na(dBalance[, c('age', 'highschool', 'business_occ')])])[table(dBalance$codeyear[!is.na(dBalance[, c('age', 'highschool', 'business_occ')])])==2])
out.cent.men.opt.strong <- rdrobust(y = subset(dBalance, period == 'strong'  & codeyear %in% keep)$avg_male_DF
                                     , x = subset(dBalance, period == 'strong'  & codeyear %in% keep)$run_var_male
                                     , covs = cbind(subset(dBalance, period == 'strong'  & codeyear %in% keep)$business_occ,
                                                    subset(dBalance, period == 'strong'  & codeyear %in% keep)$age, 
                                                    subset(dBalance, period == 'strong'  & codeyear %in% keep)$highschool)                                         
                                     , p = 1
                                     , c = 0	
                                     , cluster = subset(dBalance, period == 'strong'  & codeyear %in% keep)$codeyear
                                     , all = TRUE)
out.cent.women.opt.strong <- rdrobust(y = subset(dBalance, period == 'strong'  & codeyear %in% keep)$avg_fem_DF
                                       , x = subset(dBalance, period == 'strong'  & codeyear %in% keep)$run_var_male
                                      , covs = cbind(subset(dBalance, period == 'strong'  & codeyear %in% keep)$business_occ,
                                                     subset(dBalance, period == 'strong'  & codeyear %in% keep)$age, 
                                                     subset(dBalance, period == 'strong'  & codeyear %in% keep)$highschool)                                      
                                       , p = 1
                                       , c = 0	
                                       , cluster = subset(dBalance, period == 'strong'  & codeyear %in% keep)$codeyear
                                       , all = TRUE)

summary(out.cent.men.opt.strong)
summary(out.cent.women.opt.strong)

# Moderate
keep <- names(table(dBalance$codeyear[!is.na(dBalance[, c('merchant_occ', 'farmer_occ', 'incumbent')])])[table(dBalance$codeyear[!is.na(dBalance[, c('merchant_occ', 'farmer_occ', 'incumbent')])])==2])
out.cent.men.opt.moderate <- rdrobust(y = subset(dBalance, period == 'moderate' & codeyear %in% keep)$avg_male_DF
                                      , x = subset(dBalance, period == 'moderate' & codeyear %in% keep)$run_var_male
                                      , covs = cbind(subset(dBalance, period == 'moderate' & codeyear %in% keep)$merchant_occ, 
                                                     subset(dBalance, period == 'moderate' & codeyear %in% keep)$farmer_occ,
                                                     subset(dBalance, period == 'moderate' & codeyear %in% keep)$incumbent)                                         
                                      , p = 1
                                      , c = 0	
                                      , cluster = subset(dBalance, period == 'moderate' & codeyear %in% keep)$codeyear
                                      , all = TRUE)
out.cent.women.opt.moderate <- rdrobust(y = subset(dBalance, period == 'moderate' & codeyear %in% keep)$avg_fem_DF
                                        , x = subset(dBalance, period == 'moderate' & codeyear %in% keep)$run_var_male
                                        , covs = cbind(subset(dBalance, period == 'moderate' & codeyear %in% keep)$merchant_occ, 
                                                       subset(dBalance, period == 'moderate' & codeyear %in% keep)$farmer_occ,
                                                       subset(dBalance, period == 'moderate' & codeyear %in% keep)$incumbent)                                    
                                        , p = 1
                                        , c = 0	
                                        , cluster = subset(dBalance, period == 'moderate' & codeyear %in% keep)$codeyear
                                        , all = TRUE)
summary(out.cent.men.opt.moderate)
summary(out.cent.women.opt.moderate)

# Calculate Differences 
zOutWeak <- zDif(beta1 = out.cent.men.opt.weak$coef[3,], beta2 = out.cent.women.opt.weak$coef[3,]
                 , se1 = out.cent.men.opt.weak$se[3,], se2 = out.cent.women.opt.weak$se[3,])
zOutStrong <- zDif(beta1 = out.cent.men.opt.strong$coef[3,], beta2 = out.cent.women.opt.strong$coef[3,]
                   , se1 = out.cent.men.opt.strong$se[3,], se2 = out.cent.women.opt.strong$se[3,])
zOutModerate <- zDif(beta1 = out.cent.men.opt.moderate$coef[3,], beta2 = out.cent.women.opt.moderate$coef[3,]
                     , se1 = out.cent.men.opt.moderate$se[3,], se2 = out.cent.women.opt.moderate$se[3,])

zOutWeak
(1-pnorm(zOutWeak$zscore)) * 2 

zOutStrong
(1-pnorm(zOutStrong$zscore)) * 2 

zOutModerate
(1-pnorm(zOutModerate$zscore)) * 2 

# TABLE E.19
Estimate = rbind(out.cent.men.opt.weak$coef[3],out.cent.women.opt.weak$coef[3],zOutWeak[1],
                 out.cent.men.opt.strong$coef[3],out.cent.women.opt.strong$coef[3],zOutStrong[1],
                 out.cent.men.opt.moderate$coef[3],out.cent.women.opt.moderate$coef[3],zOutModerate[1])
p_value = rbind(out.cent.men.opt.weak$pv[3],out.cent.women.opt.weak$pv[3],(1-pnorm(zOutWeak$zscore)) * 2 ,
                out.cent.men.opt.strong$pv[3],out.cent.women.opt.strong$pv[3],(1-pnorm(zOutStrong$zscore)) * 2 ,
                out.cent.men.opt.moderate$pv[3],out.cent.women.opt.moderate$pv[3],(1-pnorm(zOutModerate$zscore)) * 2 )
h = rbind(out.cent.men.opt.weak$bws[1],out.cent.women.opt.weak$bws[1],NA,
          out.cent.men.opt.strong$bws[1],out.cent.women.opt.strong$bws[1],NA,
          out.cent.men.opt.moderate$bws[1],out.cent.women.opt.moderate$bws[1],NA)
n_co = rbind(out.cent.men.opt.weak$N_h[1],out.cent.women.opt.weak$N_h[1],NA,
             out.cent.men.opt.strong$N_h[1],out.cent.women.opt.strong$N_h[1],NA,
             out.cent.men.opt.moderate$N_h[1],out.cent.women.opt.moderate$N_h[1],NA)
n_tr = rbind(out.cent.men.opt.weak$N_h[2],out.cent.women.opt.weak$N_h[2],NA,
             out.cent.men.opt.strong$N_h[2],out.cent.women.opt.strong$N_h[2],NA,
             out.cent.men.opt.moderate$N_h[2],out.cent.women.opt.moderate$N_h[2],NA)
table_e19 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixEResults/tableE19.txt")
print("TABLE E.19: RD Effect of Co-Partisan Mayor on Men and Women Candidates Vote Share, when
the Mayor is a Man—Brazilian Federal Elections, 2000-2018 (Including Covariates that are imbalanced)")
table_e19
sink()


############ Mayor is a woman
# TABLE E.20
######### Weak Period
# Keep only balanced municipalities  
keep <- names(table(dBalance$codeyear[!is.na(dBalance$business_occ)])[table(dBalance$codeyear[!is.na(dBalance$business_occ)])==2])
b1 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$business_occ,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'& codeyear %in% keep)$codeyear)
summary(b1)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$health_occ)])[table(dBalance$codeyear[!is.na(dBalance$health_occ)])==2])
b2 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$health_occ,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b2)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b3 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$education_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b3)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b4 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$merchant_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b4)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])[table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])==2])
b5 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$farmer_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b5)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])[table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])==2])
b6 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$pubservant_occ,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b6)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])[table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])==2])
b7 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$laywer_pcc,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b7)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$incumbent)])[table(dBalance$codeyear[!is.na(dBalance$incumbent)])==2])
b8 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$incumbent,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b8)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$held_office)])[table(dBalance$codeyear[!is.na(dBalance$held_office)])==2])
b9 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$held_office,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b9)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$ran_office)])[table(dBalance$codeyear[!is.na(dBalance$ran_office)])==2])
b10 = rdrobust(y = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$ran_office,
                 x = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak'  & codeyear %in% keep)$codeyear)
summary(b10)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b11 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$age,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b11)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$married)])[table(dBalance$codeyear[!is.na(dBalance$married)])==2])
b12 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$married,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b12)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b13 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$highschool,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b13)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b14 = rdrobust(y = subset(dBalance,  period == 'weak' & codeyear %in% keep)$somecollege,
                 x = subset(dBalance,  period == 'weak' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'weak' & codeyear %in% keep)$codeyear)
summary(b14)


######### Strong Period
keep <- names(table(dBalance$codeyear[!is.na(dBalance$business_occ)])[table(dBalance$codeyear[!is.na(dBalance$business_occ)])==2])
b15 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$business_occ,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'& codeyear %in% keep)$codeyear)
summary(b15)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$health_occ)])[table(dBalance$codeyear[!is.na(dBalance$health_occ)])==2])
b16 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$health_occ,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b16)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b17 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$education_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b17)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b18 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$merchant_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b18)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])[table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])==2])
b19 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$farmer_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b19)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])[table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])==2])
b20 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$pubservant_occ,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b20)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])[table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])==2])
b21 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$laywer_pcc,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b21)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$incumbent)])[table(dBalance$codeyear[!is.na(dBalance$incumbent)])==2])
b22 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$incumbent,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b22)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$held_office)])[table(dBalance$codeyear[!is.na(dBalance$held_office)])==2])
b23 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$held_office,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b23)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$ran_office)])[table(dBalance$codeyear[!is.na(dBalance$ran_office)])==2])
b24 = rdrobust(y = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$ran_office,
                 x = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong'  & codeyear %in% keep)$codeyear)
summary(b24)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b25 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$age,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b25)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$married)])[table(dBalance$codeyear[!is.na(dBalance$married)])==2])
b26 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$married,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b26)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b27 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$highschool,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b27)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b28 = rdrobust(y = subset(dBalance,  period == 'strong' & codeyear %in% keep)$somecollege,
                 x = subset(dBalance,  period == 'strong' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'strong' & codeyear %in% keep)$codeyear)
summary(b28)

######### Moderate Period
keep <- names(table(dBalance$codeyear[!is.na(dBalance$business_occ)])[table(dBalance$codeyear[!is.na(dBalance$business_occ)])==2])
b29 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$business_occ,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'& codeyear %in% keep)$codeyear)
summary(b29)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$health_occ)])[table(dBalance$codeyear[!is.na(dBalance$health_occ)])==2])
b30 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$health_occ,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b30)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b31 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$education_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b31)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$education_occ)])[table(dBalance$codeyear[!is.na(dBalance$education_occ)])==2])
b32 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$merchant_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b32)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])[table(dBalance$codeyear[!is.na(dBalance$farmer_occ)])==2])
b33 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$farmer_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b33)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])[table(dBalance$codeyear[!is.na(dBalance$pubservant_occ)])==2])
b34 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$pubservant_occ,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b34)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])[table(dBalance$codeyear[!is.na(dBalance$laywer_pcc)])==2])
b35 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$laywer_pcc,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b35)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$incumbent)])[table(dBalance$codeyear[!is.na(dBalance$incumbent)])==2])
b36 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$incumbent,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b36)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$held_office)])[table(dBalance$codeyear[!is.na(dBalance$held_office)])==2])
b37 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$held_office,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b37)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$ran_office)])[table(dBalance$codeyear[!is.na(dBalance$ran_office)])==2])
b38 = rdrobust(y = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$ran_office,
                 x = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate'  & codeyear %in% keep)$codeyear)
summary(b38)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b39 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$age,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b39)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$married)])[table(dBalance$codeyear[!is.na(dBalance$married)])==2])
b40 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$married,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b40)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b41 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$highschool,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b41)
keep <- names(table(dBalance$codeyear[!is.na(dBalance$age)])[table(dBalance$codeyear[!is.na(dBalance$age)])==2])
b42 = rdrobust(y = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$somecollege,
                 x = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance,  period == 'moderate' & codeyear %in% keep)$codeyear)
summary(b42)


# Campaign Expenditures are only available after 2000 and many candidates do not report
# Sample size for weak period is too small
b43 = rdrobust(y = subset(dBalance2,  period == 'weak')$DESPESA,
                 x = subset(dBalance2,  period == 'weak')$run_var_fem,
                 h = 100,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2,  period == 'weak')$codeyear)
summary(b43)

b44 = rdrobust(y = subset(dBalance2,  period == 'strong')$DESPESA,
                 x = subset(dBalance2,  period == 'strong')$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2,  period == 'strong')$codeyear)
summary(b44)

b45 = rdrobust(y = subset(dBalance2,  period == 'moderate')$DESPESA,
                 x = subset(dBalance2,  period == 'moderate')$run_var_fem,
                 p = 1,
                 c = 0,
                 all = TRUE,
                 cluster = subset(dBalance2,  period == 'moderate')$codeyear)
summary(b45)

# TABLE E.20
ordering = c(1:14,43,15:28,44,29:42,45)
eval(parse(text = paste0("Estimate = rbind(", paste0(paste0("b", ordering, "[['coef']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("p_value = rbind(", paste0(paste0("b", ordering, "[['pv']][3]"), collapse = ","), ")") 
))
eval(parse(text = paste0("h = rbind(", paste0(paste0("b", ordering, "[['bws']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_co = rbind(", paste0(paste0("b", ordering, "[['N_h']][1]"), collapse = ","), ")") 
))
eval(parse(text = paste0("n_tr = rbind(", paste0(paste0("b", ordering, "[['N_h']][2]"), collapse = ","), ")") 
))
table_e20 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixEResults/tableE20.txt")
print("TABLE E.20: Individual Level Tests, Only Centralized Party Sample by Party Regimes when the Mayor is a Woman")
table_e20
sink()


# TABLE E.21
# Including Covariates
# Strong
keep <- names(table(dBalance$codeyear[!is.na(dBalance[, c('education_occ', 'merchant_occ', 'somecollege')])])[table(dBalance$codeyear[!is.na(dBalance[, c('education_occ', 'merchant_occ', 'somecollege')])])==2])
out.cent.men.opt.strong <- rdrobust(y = subset(dBalance, period == 'strong'  & codeyear %in% keep)$avg_male_DF
                                    , x = subset(dBalance, period == 'strong'  & codeyear %in% keep)$run_var_fem
                                    , covs = cbind(subset(dBalance, period == 'strong'  & codeyear %in% keep)$education_occ,
                                                   subset(dBalance, period == 'strong'  & codeyear %in% keep)$merchant_occ, 
                                                   subset(dBalance, period == 'strong'  & codeyear %in% keep)$somecollege)                                         
                                    , p = 1
                                    , c = 0	
                                    , cluster = subset(dBalance, period == 'strong'  & codeyear %in% keep)$codeyear
                                    , all = TRUE)
out.cent.women.opt.strong <- rdrobust(y = subset(dBalance, period == 'strong'  & codeyear %in% keep)$avg_fem_DF
                                      , x = subset(dBalance, period == 'strong'  & codeyear %in% keep)$run_var_fem
                                      , covs = cbind(subset(dBalance, period == 'strong'  & codeyear %in% keep)$education_occ,
                                                     subset(dBalance, period == 'strong'  & codeyear %in% keep)$merchant_occ, 
                                                     subset(dBalance, period == 'strong'  & codeyear %in% keep)$somecollege)                                      
                                      , p = 1
                                      , c = 0	
                                      , cluster = subset(dBalance, period == 'strong'  & codeyear %in% keep)$codeyear
                                      , all = TRUE)

summary(out.cent.men.opt.strong)
summary(out.cent.women.opt.strong)

# Moderate
keep <- names(table(dBalance$codeyear[!is.na(dBalance[, c('education_occ', 'pubservant_occ', 'laywer_pcc', 'held_office', 'ran_office')])])[table(dBalance$codeyear[!is.na(dBalance[, c('education_occ', 'pubservant_occ', 'laywer_pcc', 'held_office', 'ran_office')])])==2])
out.cent.men.opt.moderate <- rdrobust(y = subset(dBalance, period == 'moderate' & codeyear %in% keep)$avg_male_DF
                                      , x = subset(dBalance, period == 'moderate' & codeyear %in% keep)$run_var_fem
                                      , covs = cbind(subset(dBalance, period == 'moderate'  & codeyear %in% keep)$education_occ,
                                                     subset(dBalance, period == 'moderate'  & codeyear %in% keep)$pubservant_occ, 
                                                     subset(dBalance, period == 'moderate'  & codeyear %in% keep)$laywer_pcc,
                                                     subset(dBalance, period == 'moderate'  & codeyear %in% keep)$held_office,
                                                     subset(dBalance, period == 'moderate'  & codeyear %in% keep)$ran_office)                                       
                                      , p = 1
                                      , c = 0	
                                      , cluster = subset(dBalance, period == 'moderate' & codeyear %in% keep)$codeyear
                                      , all = TRUE)
out.cent.women.opt.moderate <- rdrobust(y = subset(dBalance, period == 'moderate' & codeyear %in% keep)$avg_fem_DF
                                        , x = subset(dBalance, period == 'moderate' & codeyear %in% keep)$run_var_fem
                                        , covs = cbind(subset(dBalance, period == 'moderate'  & codeyear %in% keep)$education_occ,
                                                       subset(dBalance, period == 'moderate'  & codeyear %in% keep)$pubservant_occ, 
                                                       subset(dBalance, period == 'moderate'  & codeyear %in% keep)$laywer_pcc,
                                                       subset(dBalance, period == 'moderate'  & codeyear %in% keep)$held_office,
                                                       subset(dBalance, period == 'moderate'  & codeyear %in% keep)$ran_office)                                  
                                        , p = 1
                                        , c = 0	
                                        , cluster = subset(dBalance, period == 'moderate' & codeyear %in% keep)$codeyear
                                        , all = TRUE)
summary(out.cent.men.opt.moderate)
summary(out.cent.women.opt.moderate)

# Calculate Differences 
zOutStrong <- zDif(beta1 = out.cent.men.opt.strong$coef[3,], beta2 = out.cent.women.opt.strong$coef[3,]
                   , se1 = out.cent.men.opt.strong$se[3,], se2 = out.cent.women.opt.strong$se[3,])
zOutModerate <- zDif(beta1 = out.cent.men.opt.moderate$coef[3,], beta2 = out.cent.women.opt.moderate$coef[3,]
                     , se1 = out.cent.men.opt.moderate$se[3,], se2 = out.cent.women.opt.moderate$se[3,])

zOutStrong
(pnorm(zOutStrong$zscore)) * 2 

zOutModerate
(1-pnorm(zOutModerate$zscore)) * 2 

# TABLE E.21
Estimate = rbind(out.cent.men.opt.strong$coef[3],out.cent.women.opt.strong$coef[3],zOutStrong[1],
                 out.cent.men.opt.moderate$coef[3],out.cent.women.opt.moderate$coef[3],zOutModerate[1])
p_value = rbind(out.cent.men.opt.strong$pv[3],out.cent.women.opt.strong$pv[3],(1-pnorm(zOutStrong$zscore)) * 2 ,
                out.cent.men.opt.moderate$pv[3],out.cent.women.opt.moderate$pv[3],(1-pnorm(zOutModerate$zscore)) * 2 )
h = rbind(out.cent.men.opt.strong$bws[1],out.cent.women.opt.strong$bws[1],NA,
          out.cent.men.opt.moderate$bws[1],out.cent.women.opt.moderate$bws[1],NA)
n_co = rbind(out.cent.men.opt.strong$N_h[1],out.cent.women.opt.strong$N_h[1],NA,
             out.cent.men.opt.moderate$N_h[1],out.cent.women.opt.moderate$N_h[1],NA)
n_tr = rbind(out.cent.men.opt.strong$N_h[2],out.cent.women.opt.strong$N_h[2],NA,
             out.cent.men.opt.moderate$N_h[2],out.cent.women.opt.moderate$N_h[2],NA)
table_e21 = cbind.data.frame(Estimate,p_value,h,n_co,n_tr)

sink("./AppendixEResults/tableE21.txt")
print("TABLE E.21: RD Effect of Co-Partisan Mayor on Men and Women Candidates Vote Share, when
the Mayor is a Woman—Brazilian Federal Elections, 2000-2018 (Including Covariates that are
imbalanced)")
table_e21
sink()


##############################
######### Sensitivity ########
##############################

# Subset to Mixed Races and parties ran both male and female candidates
d2 <- subset(d, mixed == 1 & nonZeroDF == 2)
# Create running variable
d2$run_var_fem <- ifelse(d2$position == 1 & d2$CODIGO_SEXO == 4
                         , d2$diff_cand * 1, d2$diff_cand * -1)
d2$run_var_fem[d2$mayorFem == 0] <- NA
d2$run_var_male <- ifelse(d2$position == 1 & d2$CODIGO_SEXO == 2
                          , d2$diff_cand * 1, d2$diff_cand * -1)
d2$run_var_male[d2$mayorFem == 1] <- NA

# FIGURE E.40
genSensitivityH2(yearElection = c(1996, 2000, 2004), period = 'mayorMen_weak', run_var = 'run_var_male', file = "./AppendixEResults/figE40.pdf")

# FIGURE E.41
genSensitivityH2(yearElection = c(2008, 2012), period = 'mayorMen_strong', run_var = 'run_var_male', file = "./AppendixEResults/figE41.pdf")

# FIGURE E.42
genSensitivityH2(yearElection = c(2016), period = 'mayorMen_moderate', run_var = 'run_var_male', file = "./AppendixEResults/figE42.pdf")

# FIGURE E.43
genSensitivityH2(yearElection = c(1996, 2000, 2004), period = 'mayorWomen_weak', run_var = 'run_var_fem', file = "./AppendixEResults/figE43.pdf")

# FIGURE E.44
genSensitivityH2(yearElection = c(2008, 2012), period = 'mayorWomen_strong', run_var = 'run_var_fem', file = "./AppendixEResults/figE44.pdf")

# FIGURE E.45
genSensitivityH2(yearElection = c(2016), period = 'mayorWomen_moderate', run_var = 'run_var_fem', file = "./AppendixEResults/figE45.pdf")


#################################
##### Different Polynomials #####
#################################

# TABLE E.22
sink("./AppendixEResults/tableE22.txt")
print("TABLE E.22: Effect of Co-Partisan Men Mayors on Average Vote Share (Different Polynomials)—Brazilian Federal Elections")
polyH2(yearElection = c(1996, 2000, 2004), p = 2, run_var = 'run_var_male')
polyH2(yearElection = c(1996, 2000, 2004), p = 3, run_var = 'run_var_male')
polyH2(yearElection = c(1996, 2000, 2004), p = 4, run_var = 'run_var_male')

polyH2(yearElection = c(2008, 2012), p = 2, run_var = 'run_var_male')
polyH2(yearElection = c(2008, 2012), p = 3, run_var = 'run_var_male')
polyH2(yearElection = c(2008, 2012), p = 4, run_var = 'run_var_male')

polyH2(yearElection = c(2016), p = 2, run_var = 'run_var_male')
polyH2(yearElection = c(2016), p = 3, run_var = 'run_var_male')
polyH2(yearElection = c(2016), p = 4, run_var = 'run_var_male')
sink()

# TABLE E.23
sink("./AppendixEResults/tableE23.txt")
print("TABLE E.23: RD Effect of Co-Partisan Women Mayors on Average Vote Share (Different Polynomials)—Brazilian Federal Elections")
polyH2(yearElection = c(1996, 2000, 2004), p = 2, run_var = 'run_var_fem')
polyH2(yearElection = c(1996, 2000, 2004), p = 3, run_var = 'run_var_fem')
polyH2(yearElection = c(1996, 2000, 2004), p = 4, run_var = 'run_var_fem')

polyH2(yearElection = c(2008, 2012), p = 2, run_var = 'run_var_fem')
polyH2(yearElection = c(2008, 2012), p = 3, run_var = 'run_var_fem')
polyH2(yearElection = c(2008, 2012), p = 4, run_var = 'run_var_fem')

polyH2(yearElection = c(2016), p = 2, run_var = 'run_var_fem')
polyH2(yearElection = c(2016), p = 3, run_var = 'run_var_fem')
polyH2(yearElection = c(2016), p = 4, run_var = 'run_var_fem')
sink()

############################
##### Linear Estimator #####
############################
#---------- Mayor Men
d2$winner <- as.numeric(d2$run_var_male > 0)
# Non-Loyalty
men.men.noL <- linearEstimatorH2(yearElection = c(1996, 2000, 2004), DV = 'avg_male_DF', run_var = 'run_var_male')
men.women.noL <- linearEstimatorH2(yearElection = c(1996, 2000, 2004), DV = 'avg_fem_DF', run_var = 'run_var_male')
men.noL.z <- zDif(beta1 = men.men.noL[['beta']], beta2 = men.women.noL[['beta']], se1 = men.men.noL[['se']], se2 = men.women.noL[['se']])
men.noL.pvalue <- ifelse(men.noL.z$zscore > 0, (1-pnorm(men.noL.z$zscore)) * 2, pnorm(men.noL.z$zscore) * 2)
# With loyalty
men.men.L <- linearEstimatorH2(yearElection = c(2008, 2012), DV = 'avg_male_DF', run_var = 'run_var_male')
men.women.L <- linearEstimatorH2(yearElection = c(2008, 2012), DV = 'avg_fem_DF', run_var = 'run_var_male')
men.L.z <- zDif(beta1 = men.men.L[['beta']], beta2 = men.women.L[['beta']], se1 = men.men.L[['se']], se2 = men.women.L[['se']])
men.L.pvalue <- ifelse(men.L.z$zscore > 0, (1-pnorm(men.L.z$zscore)) * 2, pnorm(men.L.z$zscore) * 2)
# 2016
men.men.L16 <- linearEstimatorH2(yearElection = c(2016), DV = 'avg_male_DF', run_var = 'run_var_male')
men.women.L16 <- linearEstimatorH2(yearElection = c(2016), DV = 'avg_fem_DF', run_var = 'run_var_male')
men.L16.z <- zDif(beta1 = men.men.L16[['beta']], beta2 = men.women.L16[['beta']], se1 = men.men.L16[['se']], se2 = men.women.L16[['se']])
men.L16.pvalue <- ifelse(men.L16.z$zscore > 0, (1-pnorm(men.L16.z$zscore)) * 2, pnorm(men.L16.z$zscore) * 2)

# Mayor Women
d2$winner <- as.numeric(d2$run_var_fem > 0)
# Non-Loyalty
women.men.noL <- linearEstimatorH2(yearElection = c(1996, 2000, 2004), DV = 'avg_male_DF', run_var = 'run_var_fem')
women.women.noL <- linearEstimatorH2(yearElection = c(1996, 2000, 2004), DV = 'avg_fem_DF', run_var = 'run_var_fem')
women.noL.z <- zDif(beta1 = women.men.noL[['beta']], beta2 = women.women.noL[['beta']], se1 = women.men.noL[['se']], se2 = women.women.noL[['se']])
women.noL.pvalue <- ifelse(women.noL.z$zscore > 0, (1-pnorm(women.noL.z$zscore)) * 2, pnorm(women.noL.z$zscore) * 2)
# With loyalty
women.men.L <- linearEstimatorH2(yearElection = c(2008, 2012), DV = 'avg_male_DF', run_var = 'run_var_fem')
women.women.L <- linearEstimatorH2(yearElection = c(2008, 2012), DV = 'avg_fem_DF', run_var = 'run_var_fem')
women.L.z <- zDif(beta1 = women.men.L[['beta']], beta2 = women.women.L[['beta']], se1 = women.men.L[['se']], se2 = women.women.L[['se']])
women.L.pvalue <- ifelse(women.L.z$zscore > 0, (1-pnorm(women.L.z$zscore)) * 2, pnorm(women.L.z$zscore) * 2)
# 2016
women.men.L16 <- linearEstimatorH2(yearElection = c(2016), DV = 'avg_male_DF', run_var = 'run_var_fem')
women.women.L16 <- linearEstimatorH2(yearElection = c(2016), DV = 'avg_fem_DF', run_var = 'run_var_fem')
women.L16.z <- zDif(beta1 = women.men.L16[['beta']], beta2 = women.women.L16[['beta']], se1 = women.men.L16[['se']], se2 = women.women.L16[['se']])
women.L16.pvalue <- ifelse(women.L16.z$zscore > 0, (1-pnorm(women.L16.z$zscore)) * 2, pnorm(women.L16.z$zscore) * 2)

# FIGURE E.46
pdf('./AppendixEResults/figE46.pdf')
plotTtest(betas = men.men.noL[['beta']], lb = men.men.noL[['lb']], ub = men.men.noL[['ub']], dvName = 'Federal Deputy - Men', yub = 10, ylb = -10)
plotTtest(betas = men.women.noL[['beta']], lb = men.women.noL[['lb']], ub = men.women.noL[['ub']], dvName = 'Federal Deputy - Women', yub = 10, ylb = -10)
plotDifpvalue(difference = men.noL.z$difference, pvalues = men.noL.pvalue)
dev.off()

# FIGURE E.47
pdf('./AppendixEResults/figE47.pdf')
plotTtest(betas = men.men.L[['beta']], lb = men.men.L[['lb']], ub = men.men.L[['ub']], dvName = 'Federal Deputy - Men', yub = 10, ylb = -10)
plotTtest(betas = men.women.L[['beta']], lb = men.women.L[['lb']], ub = men.women.L[['ub']], dvName = 'Federal Deputy - Women', yub = 10, ylb = -10)
plotDifpvalue(difference = men.L.z$difference, pvalues = men.L.pvalue)
dev.off()

# FIGURE E.48
pdf('./AppendixEResults/figE48.pdf')
plotTtest(betas = men.men.L16[['beta']], lb = men.men.L16[['lb']], ub = men.men.L16[['ub']], dvName = 'Federal Deputy - Men', yub = 10, ylb = -10)
plotTtest(betas = men.women.L16[['beta']], lb = men.women.L16[['lb']], ub = men.women.L16[['ub']], dvName = 'Federal Deputy - Women', yub = 10, ylb = -10)
plotDifpvalue(difference = men.L16.z$difference, pvalues = men.L16.pvalue)
dev.off()

# FIGURE E.49
pdf('./AppendixEResults/figE49.pdf')
plotTtest(betas = women.men.noL[['beta']], lb = women.men.noL[['lb']], ub = women.men.noL[['ub']], dvName = 'Federal Deputy - Men', yub = 10, ylb = -10)
plotTtest(betas = women.women.noL[['beta']], lb = women.women.noL[['lb']], ub = women.women.noL[['ub']], dvName = 'Federal Deputy - Women', yub = 10, ylb = -10)
plotDifpvalue(difference = women.noL.z$difference, pvalues = women.noL.pvalue)
dev.off()

# FIGURE E.50
pdf('./AppendixEResults/figE50.pdf')
plotTtest(betas = women.men.L[['beta']], lb = women.men.L[['lb']], ub = women.men.L[['ub']], dvName = 'Federal Deputy - Men', yub = 10, ylb = -10)
plotTtest(betas = women.women.L[['beta']], lb = women.women.L[['lb']], ub = women.women.L[['ub']], dvName = 'Federal Deputy - Women', yub = 10, ylb = -10)
plotDifpvalue(difference = women.L.z$difference, pvalues = women.L.pvalue)
dev.off()

# FIGURE E.51
pdf('./AppendixEResults/figE51.pdf')
plotTtest(betas = women.men.L16[['beta']], lb = women.men.L16[['lb']], ub = women.men.L16[['ub']], dvName = 'Federal Deputy - Men', yub = 10, ylb = -10)
plotTtest(betas = women.women.L16[['beta']], lb = women.women.L16[['lb']], ub = women.women.L16[['ub']], dvName = 'Federal Deputy - Women', yub = 10, ylb = -10)
plotDifpvalue(difference = women.L16.z$difference, pvalues = women.L16.pvalue)
dev.off()
